import { _decorator, Component, find, instantiate, Node, Prefab, RigidBody2D, sys, Vec2 } from 'cc';
import { Canvas } from './Canvas';
import { Permanent } from './Permanent';
const { ccclass, property } = _decorator;

@ccclass('Over')
export class Over extends Component {
    @property(Node)
    silder:Node = null

    @property(Node)
    bricks:Node = null

    @property(Prefab)
    nextpre:Prefab = null
    
    @property(Prefab)
    gameOverpre:Prefab = null

    nextnum:number = 0

    over:number = 0

    num = 0

    // 游戏结束逻辑
    start() {

    }

    // 关卡结束逻辑
    next(){
        // 读取本地缓存与全局节点记录关卡数，如果小于当前，则存入当前数据
        if(sys.localStorage.getItem('level')){
            if(Number(sys.localStorage.getItem('level'))<find('Canvas').getComponent(Canvas).level){
                sys.localStorage.setItem('level',find('Canvas').getComponent(Canvas).level.toString())
                find('Permanent').getComponent(Permanent).levelnum = find('Canvas').getComponent(Canvas).level
            }
        }
        // 停止所有白球运动
        let balls = this.silder.children
        for(let i = 0;i<balls.length;i++){
            balls[i].getComponent(RigidBody2D).linearVelocity = new Vec2(0,0)
        }

        // 实例化通过界面
        let pre = instantiate(this.nextpre)
        find('Canvas').addChild(pre)
        pre.setPosition(0,0,0)
    }

    gameOver(){
        find('Canvas').getComponent(Canvas).gameover =1
        let pre = instantiate(this.gameOverpre)
        find('Canvas').addChild(pre)
        pre.setPosition(0,0,0)
    }

    update(deltaTime: number) {

        if(this.silder.children.length == 0){
            this.gameOver()
        }

        if(this.bricks.children.length == 0){
            this.next()
        }
        
    }
}


